﻿using npljobs.JobAlertService.Objects;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace npljobs.JobAlertService.Helper
{
    /// <summary>
    /// This class is used to send email to the user those set alerts enabled
    /// </summary>
    public class SendMail
    {
        #region SendEMail
        public bool SendEMail(string sendTo, string htmlBody)
        {
            bool isMainSent = false;
            try
            {
                string emailFrom = ConfigurationManager.AppSettings["EMAIL_FROM_ADDRESS"] != null ? ConfigurationManager.AppSettings["EMAIL_FROM_ADDRESS"].ToString() : string.Empty;
                string pwdFrom = ConfigurationManager.AppSettings["EMAIL_FROM_PASSWORD"] != null ? ConfigurationManager.AppSettings["EMAIL_FROM_PASSWORD"].ToString() : string.Empty;

                string subject = ConfigurationManager.AppSettings["SUBJECT"] != null ? ConfigurationManager.AppSettings["SUBJECT"].ToString() : string.Empty;
                string host = ConfigurationManager.AppSettings["HOST"] != null ? ConfigurationManager.AppSettings["HOST"].ToString() : string.Empty;

                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient(host);

                mail.From = new MailAddress(emailFrom);
                mail.To.Add(sendTo);
                mail.Subject = subject;

                mail.IsBodyHtml = true;

                mail.Body = htmlBody;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(emailFrom, pwdFrom);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                isMainSent = true;
            }
            catch (Exception ex)
            {
                isMainSent = false;
            }
            return isMainSent;
        } 
        #endregion
    }
}
